// MessageNotificationController.java
package net.web.controller;

import net.web.common.utils.JSONUtil;
import net.web.common.utils.Result;
import net.web.entity.MessageNotification;
import net.web.service.MessageNotificationService;
import net.web.common.framework.simplemvc.RequestMapping;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

@RequestMapping("/api/notification")
public class MessageNotificationController {
    private MessageNotificationService notificationService = new MessageNotificationService();

    public void getNotificationsByUserId(HttpServletRequest req, HttpServletResponse resp) throws IOException {
        int userId = Integer.parseInt(req.getParameter("userId"));
        JSONUtil.printByJSON(resp, Result.success(notificationService.getNotificationsByUserId(userId)));
    }

    public void markAsRead(HttpServletRequest req, HttpServletResponse resp) throws IOException {
        int notificationId = Integer.parseInt(req.getParameter("notificationId"));
        if (notificationService.markAsRead(notificationId)) {
            JSONUtil.printByJSON(resp, Result.success("标记为已读成功"));
        } else {
            JSONUtil.printByJSON(resp, Result.fail("标记为已读失败"));
        }
    }

    @RequestMapping("/list.do")
    public void list(HttpServletRequest req, HttpServletResponse resp) throws IOException {
        getNotificationsByUserId(req, resp);
    }
    @RequestMapping("/read.do")
    public void read(HttpServletRequest req, HttpServletResponse resp) throws IOException {
        markAsRead(req, resp);
    }
    @RequestMapping("/unread.do")
    public void unread(HttpServletRequest req, HttpServletResponse resp) throws IOException {
        int userId = Integer.parseInt(req.getParameter("userId"));
        JSONUtil.printByJSON(resp, Result.success(notificationService.getUnreadNotificationsByUserId(userId)));
    }
}